package com.golden.procedure.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.procedure.dao.WorkOrderMapper;
import com.golden.procedure.dao.WorkOrderPhotographyMapper;
import com.golden.procedure.dao.WorkSeatSchedulingMapper;
import com.golden.procedure.domain.WorkCommentDO;
import com.golden.procedure.domain.WorkOrderBookDO;
import com.golden.procedure.domain.WorkOrderDO;
import com.golden.procedure.domain.WorkOrderPhotographyDO;
import com.golden.procedure.domain.WorkOrderPickDO;
import com.golden.procedure.domain.WorkSeatSchedulingDO;
import com.golden.procedure.domain.WorkTaskHistoryDO;
import com.golden.procedure.domain.WorkerDO;
import com.golden.procedure.entity.WorkOrderPhotography;
import com.golden.procedure.entity.WorkOrderPhotographyExample;
import com.golden.procedure.entity.WorkSeatScheduling;
import com.golden.procedure.query.SchedulingQueryDO;
import com.golden.procedure.query.WorkOrderBookQueryDO;
import com.golden.procedure.query.WorkPhotographyQueryDO;
import com.golden.procedure.query.WorkerQueryDO;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.ResultSupport;
import com.golden.procedure.service.ISiteService;
import com.golden.procedure.service.IWorkAttachmentService;
import com.golden.procedure.service.IWorkCommentService;
import com.golden.procedure.service.IWorkOrderBookService;
import com.golden.procedure.service.IWorkOrderPickService;
import com.golden.procedure.service.IWorkOrderService;
import com.golden.procedure.service.IWorkPhotographyService;
import com.golden.procedure.service.IWorkSeatSchedulingService;
import com.golden.procedure.service.IWorkSiteSeatService;
import com.golden.procedure.service.IWorkTaskHistoryService;
import com.golden.procedure.service.IWorkerService;
import com.golden.procedure.util.BeanUtilsExtends;
import com.golden.procedure.util.BonConstant;
import com.golden.procedure.util.TaskDefKey;

/**
 * 工单拍摄服务 <b>Description:</b><br>
 * 
 * @author Reading.Reagan</a>
 * @version 1.0
 * @Note <b>ProjectName:</b> procedure <br>
 *       <b>PackageName:</b> com.golden.procedure.service.impl <br>
 *       <b>ClassName:</b> WorkOrderYYService <br>
 *       <b>Date:</b> 2018年9月21日 上午11:43:45
 */
@Service("workPhotographyService")
public class WorkPhotographyService extends BaseService implements IWorkPhotographyService {

	@Autowired
	WorkOrderMapper workOrderMapper;
	@Autowired
	WorkSeatSchedulingMapper workSeatSchedulingMapper;
	@Autowired
	WorkOrderPhotographyMapper workOrderPhotographyMapper;
	@Autowired
	IWorkOrderService workOrderService;
	@Autowired
	IWorkOrderBookService workOrderBookService;
	@Autowired
	ISiteService siteService;
	@Autowired
	IWorkerService workerService;
	@Autowired
	IWorkSiteSeatService workSiteSeatService;
	@Autowired
	IWorkSeatSchedulingService workSeatSchedulingService;
	@Autowired
	IWorkCommentService workCommentService;
	@Autowired
	IWorkTaskHistoryService workTaskHistoryService;
	@Autowired
	IWorkOrderPickService workOrderPickService;
	@Autowired
	IWorkAttachmentService workAttatchmentService;

	@Override
	public ResultDO add(WorkOrderPhotographyDO domain) {
		// 初始化拍摄表（待拍摄）；
		WorkOrderPhotography photography = new WorkOrderPhotography();
		ResultDO resultDO = BeanUtilsExtends.copy(photography, domain);
		if (!resultDO.isSuccess()) {
			return resultDO;
		}
		// 订单上摄控
		photography.setShotStatus(BonConstant.SHOT_STATUS_NORMAL); // 0-未拍,1-已拍,2-重拍,3-补拍,4-改期,-1取消
		photography.setShotStatus(BonConstant.SHOT_STATUS_NORMAL); // 0-未拍,1-已拍,2-重拍,3-补拍,4-改期,-1取消
		photography.setIsDelete(BonConstant.DB_NORMAL);
		photography.setCreatetime(Calendar.getInstance().getTime());
		int ret = workOrderPhotographyMapper.insert(photography);
		if (ret < 1) {
			resultDO.setSuccess(false);
			resultDO.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			resultDO.setErrorMsg("拍摄表数据写入失败");
		}
		return resultDO;
	}

	@Override
	public ResultDO edit(WorkOrderPhotographyDO domain) {
		// 初始化拍摄表（待拍摄）；
		WorkOrderPhotography photography = new WorkOrderPhotography();
		ResultDO resultDO = BeanUtilsExtends.copy(photography, domain);
		if (!resultDO.isSuccess()) {
			return resultDO;
		}

		photography.setModifitime(Calendar.getInstance().getTime());
		int ret = 0;
		if (isNumberInValid(domain.getId())) {
			// id为空时，通过席位id以及工单id进行条件更新
			if (isNumberInValid(domain.getSeatID())) {
				resultDO.setSuccess(false);
				resultDO.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				resultDO.setErrorMsg("参数错误");
				return resultDO;
			}
			WorkOrderPhotographyExample example2 = new WorkOrderPhotographyExample();
			WorkOrderPhotographyExample.Criteria criteria2 = example2.createCriteria();
			criteria2.andSeatIDEqualTo(domain.getSeatID());
			if (!isNumberInValid(domain.getWorkOrderID())) {
				criteria2.andWorkOrderIDEqualTo(domain.getWorkOrderID());
			}
			criteria2.andIsDeleteEqualTo(BonConstant.DB_NORMAL);

			ret = workOrderPhotographyMapper.updateByExampleSelective(photography, example2);
		} else {
			// 通过id更新
			photography.setModifitime(Calendar.getInstance().getTime());
			ret = workOrderPhotographyMapper.updateByPrimaryKeySelective(photography);
		}
		if (ret < 1) {
			resultDO.setSuccess(false);
			resultDO.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			resultDO.setErrorMsg("拍摄表数据更新失败");
		}
		return resultDO;
	}

	/**
	 * 查询摄控记录列表 <b>Description:两种查询方式: 只选日期和权限时，按席位查询； 选择拍摄状态和客户姓名时，按照拍摄结果查询</b>
	 * <br>
	 * 
	 * @param queryDO
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月19日 下午4:10:05 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO querySchuduling(SchedulingQueryDO queryDO) {
		ResultDO result = new ResultSupport();

		List<WorkOrderPhotographyDO> doList = new ArrayList<WorkOrderPhotographyDO>();

		// 查询满足条件的拍摄记录
		Map<String, Object> params = new HashMap<String, Object>();
		// 上门组
		if (!isNumberInValid(queryDO.getSiteID())) {
			params.put("siteID", queryDO.getSiteID());
		}
		// 日期
		if (StringUtils.isNotEmpty(queryDO.getSchedulingDateStart())) {
			params.put("schedulingDateStart", new Date(Long.parseLong(queryDO.getSchedulingDateStart())));
			params.put("schedulingDateEnd", new Date(Long.parseLong(queryDO.getSchedulingDateEnd())));
		}
		// 姓名/电话
		if (StringUtils.isNotEmpty(queryDO.getNameOrTel())) {
			params.put("nameOrTel", "%" + queryDO.getNameOrTel() + "%");
		}

		if (queryDO.isPage()) {
			params.put("limitByPage", queryDO.getStart() + "," + queryDO.getPageRows());
			int count = workSeatSchedulingMapper.countScheduling(params);
			result.setModel(ResultDO.SECOND_MODEL_KEY, count);
		}

		List<WorkSeatScheduling> seatList = workSeatSchedulingMapper.selectScheduling(params);

		if (null == seatList || seatList.size() < 1) {
			result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
			return result;
		}

		for (WorkSeatScheduling seat : seatList) {

			WorkSeatSchedulingDO data = getWorkSeatSchedulingDO(seat);
			// 1.保存席位、日期、时段等信息
			WorkOrderPhotographyDO entity = new WorkOrderPhotographyDO();
			entity.setSiteSeatID(data.getSiteSeatID());
			entity.setSeatID(data.getId());
			entity.setSeatGroupIndex(data.getSeatGroupIndex());
			// 摄控日期&时段
			entity.setSchedulingDate(data.getSchedulingDate());
			entity.setSchedulingDateStr(data.getSchedulingDateStr());
			entity.setPlanStartOn(data.getPlanStartOn());
			entity.setPlanStartOnStr(data.getPlanStartOnStr());

			if (BonConstant.SEAT_STATUS_ENABLE == data.getStatus()) {
				// 闲置的席位，返回席位信息
				doList.add(entity);
				continue;
			}

			// 3.查询工单预约及订单信息（条件：客户姓名及手机号）
			WorkOrderBookQueryDO query1 = new WorkOrderBookQueryDO();
			query1.setSeatID(data.getId());
			ResultDO result2 = workOrderBookService.query(query1);
			if (!result2.isSuccess()) {
				// 工单及预约信息查询失败
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg("工单信息查询失败");
				result.setSuccess(false);
				return result;
			}
			List<WorkOrderBookDO> yyList = (List<WorkOrderBookDO>) result2.getModel(ResultDO.FIRST_MODEL_KEY);
			if (yyList.size() > 0) {
				entity.setWorkOrderBookDO(yyList.get(0));
			} else {
				doList.add(entity);
				continue;
			}

			// 5. 拍摄信息
			WorkOrderPhotographyExample example = new WorkOrderPhotographyExample();
			WorkOrderPhotographyExample.Criteria criteria = example.createCriteria();
			criteria.andSiteIDEqualTo(data.getSiteID());
			criteria.andSeatIDEqualTo(data.getId());
			List<WorkOrderPhotography> list = workOrderPhotographyMapper.selectByExample(example);

			if (null != list && list.size() > 0) {
				WorkOrderPhotographyDO photographyDO = getWorkOrderPhotographyDO(list.get(0));
				photographyDO.setWorkOrderBookDO(entity.getWorkOrderBookDO());

				photographyDO.setSeatGroupIndex(entity.getSeatGroupIndex());
				photographyDO.setSchedulingDate(entity.getSchedulingDate());
				photographyDO.setSchedulingDateStr(entity.getSchedulingDateStr());
				photographyDO.setPlanStartOn(entity.getPlanStartOn());
				photographyDO.setPlanStartOnStr(entity.getPlanStartOnStr());
				// 4.查询工单服务人员
				WorkerQueryDO query = new WorkerQueryDO();
				query.setSiteSeatID(data.getSiteSeatID());
				ResultDO res = workerService.queryBySiteSeat(query);
				if (res.isSuccess()) {
					List<WorkerDO> workerList = (List<WorkerDO>) res.getModel(ResultDO.FIRST_MODEL_KEY);
					photographyDO.setWorkerList(workerList);
				}
				doList.add(photographyDO);
			} else {
				doList.add(entity);
				continue;
			}
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
		return result;
	}

	/**
	 * 查询拍摄列表 <b>Description:两种查询方式: 只选日期和权限时，按席位查询； 选择拍摄状态和客户姓名时，按照拍摄结果查询</b>
	 * <br>
	 * 
	 * @param queryDO
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月19日 下午4:10:05 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO query(WorkPhotographyQueryDO queryDO) {
		ResultDO result = new ResultSupport();

		List<WorkOrderPhotographyDO> doList = new ArrayList<WorkOrderPhotographyDO>();

		// 查询满足条件的拍摄记录
		Map<String, Object> params = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(queryDO.getSchedulingDateStr())) {
			params.put("schedulingDate", new Date(Long.parseLong(queryDO.getSchedulingDateStr())));
		}
		// 当前用户id
		if (!isNumberInValid(queryDO.getEmployeeID())) {
			params.put("employeeID", queryDO.getEmployeeID());
		}
		if (!isNumberInValid(queryDO.getOrgID())) {
			// 获取所有下级子部门
			List<Integer> ids = getOrgIDs(queryDO.getOrgID());
			params.put("orgIDs", ids);
		}
		// 客户姓名/电话
		if (StringUtils.isNotEmpty(queryDO.getNameOrTel())) {
			params.put("nameOrTel", "%" + queryDO.getNameOrTel() + "%");
		}
		// 1:不看已经拍摄完成
		if (null != queryDO.getStatus() && queryDO.getStatus() == 1) {
			params.put("shotStatus", queryDO.getStatus());
		}

		if (queryDO.isPage()) {
			params.put("limitByPage", queryDO.getStart() + "," + queryDO.getPageRows());
			int count = workSeatSchedulingMapper.countPhotography(params);
			result.setModel(ResultDO.SECOND_MODEL_KEY, count);
		}

		List<WorkSeatScheduling> seatList = workSeatSchedulingMapper.selectPhotography(params);
		if (null == seatList || seatList.size() < 1) {
			// 返回空
			result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
			result.setModel(ResultDO.THIRD_MODEL_KEY, doList.size());
			return result;
		}

		int workOrderNum = 0; // 工单数量
		for (WorkSeatScheduling data : seatList) {

			WorkSeatSchedulingDO seat = getWorkSeatSchedulingDO(data);
			// 1.保存席位、日期、时段等信息
			WorkOrderPhotographyDO entity = new WorkOrderPhotographyDO();
			entity.setSiteSeatID(seat.getSiteSeatID());
			entity.setSeatID(seat.getId());
			entity.setSeatGroupIndex(seat.getSeatGroupIndex());
			entity.setVechileCode(seat.getVechileCode());
			entity.setServiceClassName(seat.getServiceClassName());
			// 日期格式转换（摄控日期&时段）
			entity.setSchedulingDate(seat.getSchedulingDate());
			entity.setSchedulingDateStr(seat.getSchedulingDateStr());
			entity.setPlanStartOn(seat.getPlanStartOn());
			entity.setPlanStartOnStr(seat.getPlanStartOnStr());

			if (BonConstant.SEAT_STATUS_ENABLE == seat.getStatus()) {
				// 闲置的席位，返回席位信息
				doList.add(entity);
				continue;
			}

			// 3.查询工单预约及订单信息
			WorkOrderBookQueryDO query1 = new WorkOrderBookQueryDO();
			query1.setSeatID(seat.getId());
			ResultDO result2 = workOrderBookService.query(query1);
			if (!result2.isSuccess()) {
				// 工单及预约信息查询失败
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg("工单信息查询失败");
				result.setSuccess(false);
				return result;
			}
			List<WorkOrderBookDO> yyList = (List<WorkOrderBookDO>) result2.getModel(ResultDO.FIRST_MODEL_KEY);
			if (yyList.size() > 0) {
				entity.setWorkOrderBookDO(yyList.get(0));
			} else {
				doList.add(entity);
				continue;
			}

			// 5. 拍摄信息
			WorkOrderPhotographyExample example = new WorkOrderPhotographyExample();
			WorkOrderPhotographyExample.Criteria criteria = example.createCriteria();
			criteria.andSeatIDEqualTo(seat.getId());
			criteria.andWorkOrderIDEqualTo(entity.getWorkOrderBookDO().getWorkOrderID());
			// criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);
			example.setOrderByClause(" id desc ");
			List<WorkOrderPhotography> list = workOrderPhotographyMapper.selectByExample(example);

			if (null == list || list.size() < 1) {
				// 工单及预约信息查询失败
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg("拍摄记录信息查询失败");
				result.setSuccess(false);
				return result;
			}
			WorkOrderPhotographyDO photographyDO = getWorkOrderPhotographyDO(list.get(0));
			photographyDO.setWorkOrderBookDO(entity.getWorkOrderBookDO());

			photographyDO.setSeatGroupIndex(entity.getSeatGroupIndex());
			photographyDO.setSchedulingDate(entity.getSchedulingDate());
			photographyDO.setSchedulingDateStr(entity.getSchedulingDateStr());
			photographyDO.setPlanStartOn(entity.getPlanStartOn());
			photographyDO.setPlanStartOnStr(entity.getPlanStartOnStr());
			photographyDO.setVechileCode(entity.getVechileCode());
			photographyDO.setServiceClassName(entity.getServiceClassName());

			// 4.有工单时查询工单服务人员
			WorkerQueryDO query = new WorkerQueryDO();
			query.setSiteSeatID(seat.getSiteSeatID());
			query.setRoleCode(BonConstant.ROLE_SHEYING);
			ResultDO res = workerService.queryBySiteSeat(query);
			if (res.isSuccess()) {
				List<WorkerDO> workerList = (List<WorkerDO>) res.getModel(ResultDO.FIRST_MODEL_KEY);
				photographyDO.setWorkerList(workerList);
			}
			// 5.查询拍摄照片数量(完成拍摄、补拍)
			if (photographyDO.getShotStatus() == BonConstant.SHOT_STATUS_OK
					|| photographyDO.getShotStatus() == BonConstant.SHOT_STATUS_RESHOT) {
				photographyDO.setTakePhotoNum(workAttatchmentService.countShotNum(photographyDO.getId()));
			}

			doList.add(photographyDO);
			workOrderNum++;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
		result.setModel(ResultDO.THIRD_MODEL_KEY, workOrderNum);
		return result;
	}

	@Override
	public ResultDO record(WorkCommentDO domain) {
		ResultDO result = new ResultSupport();

		// 1. 数据验证
		if (isNumberInValid(domain.getId()) || isNumberInValid(domain.getStatus())) {
			// 席位查询失败
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}

		// 2. 更新拍摄记录表：拍摄状态
		WorkOrderPhotography entity = workOrderPhotographyMapper.selectByPrimaryKey(domain.getId());
		if (entity.getShotStatus() == BonConstant.SHOT_STATUS_OK) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("拍摄任务已完成");
			result.setSuccess(false);
			return result;
		}

		// 更新拍摄状态
		// int takePhotoNum =
		// workAttatchmentService.countShotNum(entity.getWorkOrderID(),
		// entity.getId());
		// entity.setTakePhotoNum(takePhotoNum); // 照片数量
		entity.setShotStatus(domain.getStatus());
		entity.setShotDate(new Date());
		entity.setModifitime(Calendar.getInstance().getTime());
		entity.setModifiID(domain.getAuthorID());
		entity.setModifi(domain.getAuthor());

		int ret = workOrderPhotographyMapper.updateByPrimaryKeySelective(entity);
		if (ret < 1) {
			// 拍摄记录状态修改失败
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("拍摄记录状态修改失败");
			result.setSuccess(false);
			return result;
		}
		// TODO: 3. activity完成此节点任务

		// 4. 更新工单表：当前任务节点
		WorkOrderDO workOrder = new WorkOrderDO();
		String statusName = "拍摄完成";
		// 拍摄状态：0-未拍,1-已拍（完成拍摄）,2-重拍,3-补拍,4-改期,-1取消
		if (domain.getStatus() == BonConstant.SHOT_STATUS_OK) {
			// 完成拍摄
			workOrder.setTaskDefID(TaskDefKey.SELECT_PHOTO.getKey()); // 下一步
		} else {
			// 未完成拍摄
			workOrder.setOrderStatus(domain.getStatus());
			if (domain.getStatus() == BonConstant.SHOT_STATUS_REPHOTOGRAPH) {
				// 重拍
				workOrder.setIsRePhotograph(BonConstant.STATUS_TRUE);
				workOrder.setTaskDefID(TaskDefKey.ORDER_SCHEDULE.getKey()); // 当前任务：上摄控
				statusName = "待重拍";
			} else if (domain.getStatus() == BonConstant.SHOT_STATUS_RESHOT) {
				// 补拍
				workOrder.setIsReshot(BonConstant.STATUS_TRUE);
				workOrder.setTaskDefID(TaskDefKey.ORDER_SCHEDULE.getKey()); // 当前任务：上摄控
				statusName = "待补拍";
			} else if (domain.getStatus() == BonConstant.SHOT_STATUS_CHANGEDATE) {
				// 改期17909 - 5853 = 12056
				workOrder.setIsChangeDate(BonConstant.STATUS_TRUE);
				workOrder.setTaskDefID(TaskDefKey.ORDER_SCHEDULE.getKey()); // 当前任务：上摄控
				statusName = "拍摄改期";
			} else if (domain.getStatus() == BonConstant.SHOT_STATUS_CANCLE) {
				// 取消
				// workOrder.setStatus(BonConstant.STATUS_INVALID); // 取消
				// workOrder.setOrderStatus(BonConstant.STATUS_INVALID); // 取消
				workOrder.setOrderStatus(BonConstant.ORDER_STATUS_CANCEL); // 取消
				workOrder.setTaskDefID(TaskDefKey.ORDER_SCHEDULE.getKey()); // 当前任务：上摄控
				statusName = "拍摄取消";
			} else {
				// TODO： 未完成拍摄
				statusName = "未完成拍摄";
			}
		}
		workOrder.setId(entity.getWorkOrderID());
		workOrder.setModifiID(domain.getAuthorID());
		workOrder.setModifi(domain.getAuthor());
		ResultDO resultDO1 = workOrderService.edit(workOrder);
		if (!resultDO1.isSuccess()) {
			// 拍摄记录状态修改失败
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("工单状态修改失败");
			result.setSuccess(false);
			return result;
		}

		// 5.写入工单任务记录表、工单执行备注表
		// 5.1备注表
		WorkCommentDO commentDO = new WorkCommentDO();
		commentDO.setWorkOrderID(entity.getWorkOrderID());
		commentDO.setProcessInstanceID(entity.getProcessInstanceID());
		commentDO.setTaskDefID(TaskDefKey.UPLOAD_PHOTO.getKey()); // 拍摄
		commentDO.setTaskName(TaskDefKey.UPLOAD_PHOTO.getName());
		commentDO.setTaskID(entity.getTaskID()); // 从acitivti获取
		commentDO.setType(BonConstant.COMMENT_TYPE_REMARK); // 0备注；1事件
		if (StringUtils.isNotEmpty(domain.getRemark())) {
			commentDO.setRemark(statusName + ":" + domain.getRemark());
		} else {
			commentDO.setRemark(statusName);
		}
		commentDO.setAuthorID(domain.getAuthorID());
		commentDO.setAuthor(domain.getAuthor());
		workCommentService.add(commentDO);

		// 5.2工单任务记录表
		WorkTaskHistoryDO taskHistoryDO = new WorkTaskHistoryDO();
		taskHistoryDO.setWorkOrderID(entity.getWorkOrderID());
		taskHistoryDO.setProcessInstanceID(entity.getProcessInstanceID());
		taskHistoryDO.setTaskDefID(TaskDefKey.UPLOAD_PHOTO.getKey()); // 拍摄
		taskHistoryDO.setTaskName(TaskDefKey.UPLOAD_PHOTO.getName());
		taskHistoryDO.setTaskID(entity.getTaskID()); // 从acitivti获取
		taskHistoryDO.setSchedulingdate(entity.getShotDate());
		taskHistoryDO.setEndOn(Calendar.getInstance().getTime());
		taskHistoryDO.setStatus(domain.getStatus() != BonConstant.SHOT_STATUS_OK ? 0 : 1);
		taskHistoryDO.setAuthorID(domain.getAuthorID());
		taskHistoryDO.setAuthor(domain.getAuthor());
		workTaskHistoryService.add(taskHistoryDO);

		// TODO： 拍摄完成，并上传照片后时，写入选片表
		if (domain.getStatus() == BonConstant.SHOT_STATUS_OK) {
			// TODO: 获取当前任务id

			// 初始化选片表数据（未开始）
			WorkOrderPickDO pickDO = new WorkOrderPickDO();
			pickDO.setWorkOrderID(entity.getWorkOrderID());
			pickDO.setProcessInstanceID(entity.getProcessInstanceID());
			// TODO： activiti任务ID
			pickDO.setTaskID("");
			pickDO.setWorkPhotographyID(entity.getId());
			pickDO.setSiteSeatID(entity.getSiteSeatID());
			pickDO.setSeatID(entity.getSeatID());
			// 上门组id及名称
			pickDO.setPlaceID(entity.getSiteID());
			pickDO.setPickPlace(siteService.getName(entity.getSiteID()));
			pickDO.setAuthorID(domain.getAuthorID());
			pickDO.setAuthor(domain.getAuthor());
			workOrderPickService.add(pickDO);
		}

		return result;
	}

	@Override
	public ResultDO delete(List<Integer> seatIDs) {
		// 取消拍摄
		ResultDO resultDO = new ResultSupport();
		if (null == seatIDs || seatIDs.size() < 1) {
			resultDO.setErrorCode(ResultDO.PARAM_ERROR);
			resultDO.setErrorMsg("参数错误");
			resultDO.setSuccess(false);
			return resultDO;
		}

		WorkOrderPhotographyExample example = new WorkOrderPhotographyExample();
		WorkOrderPhotographyExample.Criteria criteria = example.createCriteria();
		criteria.andSeatIDIn(seatIDs);
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);

		WorkOrderPhotography entity = new WorkOrderPhotography();
		entity.setShotStatus(BonConstant.SHOT_STATUS_CANCLE); // 状态为-1
		entity.setIsDelete(BonConstant.DB_DELETE); // 取消预约
		entity.setModifitime(Calendar.getInstance().getTime());

		int ret = workOrderPhotographyMapper.updateByExampleSelective(entity, example);
		if (ret < 1) {
			resultDO.setSuccess(false);
			resultDO.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			resultDO.setErrorMsg("拍摄表数据更新失败");
		}
		return resultDO;
	}

	@Override
	public ResultDO get(Integer id) {
		ResultDO resultDO = new ResultSupport();
		if (isNumberInValid(id)) {
			resultDO.setErrorCode(ResultDO.PARAM_ERROR);
			resultDO.setErrorMsg("参数错误");
			resultDO.setSuccess(false);
			return resultDO;
		}

		WorkOrderPhotography entity = workOrderPhotographyMapper.selectByPrimaryKey(id);
		if (null == entity) {
			resultDO.setErrorCode(ResultDO.PARAM_ERROR);
			resultDO.setErrorMsg("拍摄记录不存在");
			resultDO.setSuccess(false);
			return resultDO;
		}
		resultDO.setModel(ResultDO.FIRST_MODEL_KEY, getWorkOrderPhotographyDO(entity));
		return resultDO;
	}

}
